<-- back

Count And Say

Link

Simply iterate through the array counting the number of times a single digit appears and then append it to a stringbuilder.

Full Solution in Java:

public class Solution { public String countAndSay(int n) { String s = "1"; for(int iter = 1; iter< n; iter++){ StringBuilder base = new StringBuilder(); int count = 1; for(int i=0; i< s.length()-1; i++){ if(s.charAt(i)==s.charAt(i+1)){ count++; } else{ base.append(count); base.append(s.charAt(i)); count = 1; } } base.append(count); base.append(s.charAt(s.length()-1)); s = base.toString(); } return s; } }